<?php
class url extends Mod
{
	public $conn;
	public $name = 'url';
	
	public function __construct()
	{

	}
	
	public function index()
	{
		$data[] = '';
		$this->render('url');
		
	}
	
	public function view()
	{
		$ip_long = array(
			array('607649792', '608174079'), //36.56.0.0-36.63.255.255
			array('975044608', '977272831'), //58.30.0.0-58.63.255.255
			array('999751680', '999784447'), //59.151.0.0-59.151.127.255
			array('1019346944', '1019478015'), //60.194.0.0-60.195.255.255
			array('1038614528', '1039007743'), //61.232.0.0-61.237.255.255
			array('1783627776', '1784676351'), //106.80.0.0-106.95.255.255
			array('1947009024', '1947074559'), //116.13.0.0-116.13.255.255
			array('1987051520', '1988034559'), //118.112.0.0-118.126.255.255
			array('2035023872', '2035154943'), //121.76.0.0-121.77.255.255
			array('2078801920', '2079064063'), //123.232.0.0-123.235.255.255
			array('-1950089216', '-1948778497'), //139.196.0.0-139.215.255.255
			array('-1425539072', '-1425014785'), //171.8.0.0-171.15.255.255
			array('-1236271104', '-1235419137'), //182.80.0.0-182.92.255.255
			array('-770113536', '-768606209'), //210.25.0.0-210.47.255.255
			array('-569376768', '-564133889'), //222.16.0.0-222.95.255.255
		);
		$rand_key = mt_rand(0, 14);
		$ramdom_ip= long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1]));
		//来易
		$user_id = 'u_5e57b9f0cc90f_Ig6Jtb5ukR';
		
		// 疫情
		$url = 'https://app1ucidpb35787.h5.xiaoeknow.com/content_page/eyJ0eXBlIjoxMiwicmVzb3VyY2VfdHlwZSI6NCwicmVzb3VyY2VfaWQiOiJsXzVlNGI3YWE0Yzg3ZWZfeG5RUlBWZUIiLCJwcm9kdWN0X2lkIjoiIiwiYXBwX2lkIjoiYXBwMVVDaWRwQjM1Nzg3IiwiZXh0cmFfZGF0YSI6MH0';
		$cookie = 'dataUpJssdkCookie={"wxver":"7.0.10","net":"WIFI","sid":""}; ko_token=bdd0a988851f224828b4148776c1b7a5; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22170cce17ff22f3-090c7c8dd03f378-7a750b14-995796-170cce17ff32e0%22%2C%22%24device_id%22%3A%22170cce17ff22f3-090c7c8dd03f378-7a750b14-995796-170cce17ff32e0%22%2C%22props%22%3A%7B%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D; sa_jssdk_2015_app1ucidpb35787=%7B%22distinct_id%22%3A%22u_5e5ca816d0f3d_IXV0Yo1qDR%22%2C%22%24device_id%22%3A%22170cce17ff22f3-090c7c8dd03f378-7a750b14-995796-170cce17ff32e0%22%2C%22props%22%3A%7B%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%2C%22first_id%22%3A%22170cce17ff22f3-090c7c8dd03f378-7a750b14-995796-170cce17ff32e0%22%7D';
		$referer = 'http://app1ucidpb35787.h5.xiaoeknow.com/v1/course/alive/l_5e4b7aa4c87ef_xnQRPVeB?type=2&app_id=app1UCidpB35787&pro_id=p_5e4fa933015f2_8BDC0AfS&available=true&share_user_id=u_5e4d4ad95307a_N2s406Ds3Q&share_type=5&scene=%E5%88%86%E4%BA%AB&is_redirect=1';
		
		$ch = curl_init($url);
		$header = array(
			'CLIENT-IP:' . $ramdom_ip,
			'X-FORWORDED-FOR:' . $ramdom_ip,
			'ACCEPPT:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
			'Connection: Keep-Alive',
			'Accept-Language: zh-cn',
			'Accept-Encoding: gzip, deflate, br',
			'Cache-Control: no-cache'
		);
		curl_setopt($ch, CURLOPT_HEADER, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (iPad; CPU OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/7.0.10(0x17000a21) NetType/WIFI Language/zh_CN');
		//curl_setopt($ch, CURLOPT_POST, false);
		curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		curl_setopt($ch, CURLOPT_REFERER, $referer); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
		$result = curl_exec($ch); 
		curl_close($ch);
		var_dump($result);
	}
	
	public function table()
	{
		$data = [];
		$this->render('table', $data, $this->name);
	}
	
	
	
	public function ajax_table_info()
	{
		$tables = $this->conn->tables();
		$res = [
			'code' => 0,
			'count' => count($tables),
			'data' => $tables
		];
		die(json_encode($res));
	}
	
	public function ajax_table_detail()
	{
		$tname = Request::getParam('t_name');
		$data = $this->conn->table($tname);
		$res = [
			'code' => 0,
			'table' => $tname,
			'data' => $data,
			'count' => count($data)
		];
		die(json_encode($res));
	}
	
	public function ajax_add_field()
	{
		$data['name'] = Request::postParam('field_name');
		if (empty($data['name'])) $data['name'] = 'field_' . substr(md5(time()), 0, 6);
		$data['type'] = Request::postParam('field_type', 'text');
		$data['isrequired'] = Request::postParam('field_required', 0);
		$data['isprimary'] = Request::postParam('field_primary', 0);
		$data['default'] = Request::postParam('field_default');
		if (empty($data['default'])) $data['isdefault'] = 0;
		die(json_encode($data));
	}
	
	public function signin()
	{
		$this->render('signin');
	}
	
	public function m_signin()
	{
		$this->render('msignin');
	}
	
	public function search()
	{
		$keywords = isset($_GET['k']) ? Unit::safe($_GET['k']) : 'camelliae';
		$data['keywords'] = $keywords;
		$this->render('search', $data);
	}
	
	
	
	public function create_token()
	{
		/*while($i < 498) {
			$i ++;
			$sqlstr = "insert into user (add_time, token) values (" . time() . ",'" . rand(1001, 9998) . "')";
			$this->conn->sql = $sqlstr;
			$res = $this->conn->execute();
			var_dump($res);
		}*/
		
		
	}
	
	public function get_list()
	{
		$status = isset($_GET['s']) ? Unit::safe($_GET['s']) : -1;
		$keywords = isset($_GET['k']) ? Unit::safe($_GET['k']) : '';
		$whereStr = '';
		if ($status > -1) {
			$whereStr .= " AND `status`='$status'";
		}
		if ( ! empty($keywords)) {
			$whereStr .= " AND (`name` LIKE '%$keywords%' OR `inviter` LIKE '%$keywords%' OR `tnumber` LIKE '%$keywords%' OR `mobile` LIKE '%$keywords%' OR `token` LIKE '%$keywords%')";
		}
		$this->conn->sql = "SELECT * FROM user WHERE 1=1" . $whereStr;
		$queryData = $this->conn->rows();
		$count = $this->conn->count();
		$res = ['code' => 0, 'msg' => '返回成功', 'count' => $count];
		$res['data'] = $queryData;
		die(json_encode($res));
	}
	
	public function get_profile()
	{
		$res = ['code' => 0, 'msg' => '返回成功'];
		$token = isset($_POST['t']) ? Unit::safe($_POST['t']) : 1000;
		if (is_numeric($token) && $token > 1000 && $token < 10000) {
			$sqlstr = "SELECT * FROM user WHERE token='$token'";
			$this->conn->sql = $sqlstr;
			$queryData = $this->conn->row();
			$res['data'] = $queryData;
		}
		die(json_encode($res));
	}
	
	public function save()
	{
		$res = ['code' => 0, 'msg' => '返回成功'];
		$id = $_POST['id'];
		$field = $_POST['field'];
		$value = $_POST['value'];
		$sets = ",edit_time='" . time() . "'";
		if ($field == 'status' && $value == 1) {
			$sets .= ",confirm_time='" . time() . "'";
		}
		$sqlstr = "UPDATE user SET $field='$value'" . $sets . " WHERE id=$id";
		//$res['sql'] = $sqlstr;
		$this->conn->sql = $sqlstr;
		$exec = $this->conn->execute();
		$res['status'] = $exec['code'];
		$res['msg'] = $exec['msg'];
		die(json_encode($res));
	}
	
}